package com.cms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.TrLotteryPlayInfo;
import com.cms.service.PlaySettingService;
import com.cms.service.ReportService;
import com.google.gson.Gson;

@Controller
public class PlaySettingController extends BaseController {

	@Autowired
	private ReportService reportService;

	@Autowired
	private PlaySettingService settingService;

	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/playSetting.do")
	public String enter(HttpServletRequest req, HttpServletResponse resp,
			ModelMap map) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put(CmsConstant.CATALOG_NAME, "LOTTERY_TYPE");
		map.put("lotteryTypes", this.reportService.getReportResult(
				CmsConstant.CATALOG_CODE, param));
		return "play/playSettingMain.html";
	}

	/**
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/playSetting/queryLotteryPlay.do")
	public String queryLotteryPlay(HttpServletRequest req,
			HttpServletResponse resp, ModelMap map) throws Exception {
		Map<String, Object> param = this.buildQueryMap(req);
		map.put("lotteryPlaySettings", this.reportService.getReportResult(
				"querylotteryPlaySettings", param));
		return "play/playSettingList.html";
	}

	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/playSetting/updatePlayGroupStatus.do")
	public void updatePlayGroup(HttpServletRequest req,
			HttpServletResponse resp, ModelMap map) throws Exception {
		int groupId = StringUtils.str2Int(req.getParameter("groupId"));
		String status = req.getParameter("status");
		Map<String, Object> param = new HashMap<String, Object>();
		try {
			this.settingService.updatePlayGroupStatus(groupId, status);
			param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			param.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(param));
	}

	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/playSetting/updatePlayStatus.do")
	public void updatePlayStatus(HttpServletRequest req,HttpServletResponse resp, ModelMap map) throws Exception 
	{
		int playId = StringUtils.str2Int(req.getParameter("playId"));
		int groupId = StringUtils.str2Int(req.getParameter("groupId"));
		String status = req.getParameter("status");
		Map<String, Object> param = new HashMap<String, Object>();
		try {
			this.settingService.updatePlayStatus(playId, status,groupId);
			param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			param.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(param));
	}

	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/playSetting/updatePlayInfo.do")
	public void updatePlayInfo(HttpServletRequest req,
			HttpServletResponse resp, ModelMap map) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		try {
			this.settingService.updatePlayInfo(this.buildQueryMap(req));
			param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			param.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(param));
	}

	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/playSetting/toEditPlaySetting.do")
	public String toEditPlaySetting(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		int playId = StringUtils.str2Int(req.getParameter("playId"));
		map.put("playSetting", this.settingService.getPlayInfo(playId));
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("playId", playId);
		map.put("playGroups", this.reportService.getReportResult("queryPlayGroup", param));
		return "play/playSettingInfo.html";
	}
	
	/***
	 * 
	 * @param req
	 * @param resp
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/playSetting/updatePlaySetting.do")
	public void updatePlaySetting(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		TrAdminUser tu = this.getLoginUser(req);
		try {
			TrLotteryPlayInfo info = new TrLotteryPlayInfo();
			info.setModifyUser(tu.getId());
			this.export(req, "playSetting", info);
			this.settingService.updateLotteryPlay(info);
			param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			param.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(param));
	}
}
